 //
//  YHEmotcionTools.swift
//  YHWeibo
//
//  Created by yuhao.mac on 16/7/5.
//  Copyright © 2016年 yuhao.mac. All rights reserved.
//

import UIKit
 
//显示的列数
let EmoticonMaxCol = 7
//显示的行数
let EmoticonMaxRow = 3
//每页显示的个数
let EmoticonMaxCount = EmoticonMaxCol * EmoticonMaxRow - 1

class YHEmotcionTools: NSObject {
    
    static let sharedTools: YHEmotcionTools = YHEmotcionTools()
    
    //用懒加载
    lazy var emotcionBundle:NSBundle = {
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        
        //返回bundle
        return NSBundle(path: path)!
    }()

    
    //default 表情 一维数组
    lazy var defaultEmoticons:[YHEmotcion] = {
        return self.getEmotcions("default/")
    }()
    //emoji 表情 一维数组
    lazy var emojiEmoticons:[YHEmotcion] = {
        return self.getEmotcions("emoji/")
    }()
    //浪小花 表情 一维数组
    lazy var lxhEmoticons:[YHEmotcion] = {
        return self.getEmotcions("lxh/")
    }()
    
    
    //表情三维数组
    lazy var allEmoticons:[[[YHEmotcion]]] = {
        return [
            self.getEmoticonsGroup(self.defaultEmoticons),
            self.getEmoticonsGroup(self.emojiEmoticons),
            self.getEmoticonsGroup(self.lxhEmoticons)
        ]
    }()
    
    func text(){
        self.getEmoticonsGroup(self.lxhEmoticons)
    }
    
    //读取plist文件中的数组转成一维模型数组
    func getEmotcions(fileName:String) -> [YHEmotcion]{
        let file = emotcionBundle.pathForResource("\(fileName)info.plist", ofType: nil)!
        //将plist转成数组 OC的方法
        let array = NSArray(contentsOfFile: file)!
        
        //创建临时数组保存数据
        var tempArr:[YHEmotcion] = [YHEmotcion]()
        
        //遍历array
        for dic in array{
            let emo = YHEmotcion(dict: dic as![String: AnyObject])
            
            emo.path = "\(fileName)"
            
            tempArr.append(emo)
        }
        return tempArr
    }
    
    //一维数组转二维数组
    func getEmoticonsGroup(emoticon:[YHEmotcion]) -> [[YHEmotcion]]{
        //计算页数
        let pageCount = (emoticon.count + EmoticonMaxCount - 1) / EmoticonMaxCount
        
        //创建一个临时的可变二维数组
        var tempArr = [[YHEmotcion]]()
        
        //遍历pageCount
        for i in 0..<pageCount{
            //截取数组，截取位置和长度
            let loc = EmoticonMaxCount * i
            var len = EmoticonMaxCount
            
            //防止默认表情数组越界
            if loc + len > emoticon.count{
                len = emoticon.count - loc
            }
            
            let range = NSMakeRange(loc, len)
            
            //截取数组
            let arr = (emoticon as NSArray).subarrayWithRange(range) as![YHEmotcion]
            
            //保存
            tempArr.append(arr)
        }
        return tempArr
    }
}
 
 
 
 
 
 
 
 
 
 

 
 
 
 
 
